package com.example.onlinetaxi.ui.activity.Driver.MainPage;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.IExchangeFragments;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.view.Dialog.LoadingDialog;

public class DriverMainActivity extends BaseActivity implements View.OnClickListener {

    // 导航栏控件中的布局、图片和按钮
    // 导航叫车界面
    private LinearLayout carLayout;
    private ImageView carImg;
    private TextView carTv;
    // 预定叫车界面
    private LinearLayout orderLayout;
    private ImageView orderImg;
    private TextView orderTv;
    // 个人信息界面
    private LinearLayout mineLayout;
    private ImageView mineImg;
    private TextView mineTv;
    // fragments
    private DriverMapFragment driverMapFragment;
    private DriverOrderFragment driverOrderFragment;
    private DriverMineFragment driverMineFragment;
    // 记录上一次跳转
    private Integer lastJump = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_main);

        initView();

        initEvent();
    }

    // 事件初始化
    private void initEvent() {
        // 司机其他界面，此时叫到车，回到叫车页面
        driverMapFragment.setiExchangeFragments(new IExchangeFragments() {
            @Override
            public void exchangeFragment() {
                // 设置底部导航栏样式
                carTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                carImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                orderTv.setTextColor(Color.GRAY);
                orderImg.setColorFilter(Color.GRAY);
                mineTv.setTextColor(Color.GRAY);
                mineImg.setColorFilter(Color.GRAY);
                driverMapFragment.setFirstLocation();  // 设置定位初始化
                // 跳转回到初始页面
                replaceFragment(driverMapFragment);
                lastJump = 1;
            }
        });

        driverMineFragment.setiExchangeFragments(new IExchangeFragments() {
            @Override
            public void exchangeFragment() {
                // 进行按钮样式的修改
                orderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                orderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                carTv.setTextColor(Color.GRAY);
                carImg.setColorFilter(Color.GRAY);
                mineTv.setTextColor(Color.GRAY);
                mineImg.setColorFilter(Color.GRAY);
                // 跳转到订单页面
                replaceFragment(driverOrderFragment);
                lastJump = 2;
            }
        });

        // 设置初始界面
        lastJump = 1;
        replaceFragment(driverMapFragment);

        // 设置点击动作
        carLayout.setOnClickListener(this);
        orderLayout.setOnClickListener(this);
        mineLayout.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.driver_navigate_car_layout:
                if (lastJump != 1) {
                    // 进行按钮样式的修改
                    carTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                    carImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                    orderTv.setTextColor(Color.GRAY);
                    orderImg.setColorFilter(Color.GRAY);
                    mineTv.setTextColor(Color.GRAY);
                    mineImg.setColorFilter(Color.GRAY);
                    driverMapFragment.setFirstLocation();  // 设置定位初始化
                    // 显示加载框
                    LoadingDialog.showLoading(this, "正在加载地图...");
                    replaceFragment(driverMapFragment);
                    lastJump = 1;
                }
                break;
            case R.id.driver_navigate_order_layout:
                if (lastJump != 2) {
                    // 进行按钮样式的修改
                    orderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                    orderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                    carTv.setTextColor(Color.GRAY);
                    carImg.setColorFilter(Color.GRAY);
                    mineTv.setTextColor(Color.GRAY);
                    mineImg.setColorFilter(Color.GRAY);
                    // 显示加载框
                    LoadingDialog.showLoading(this, "正在加载订单信息...");
                    replaceFragment(driverOrderFragment);
                    lastJump = 2;
                }
                break;
            case R.id.driver_navigate_mine_layout:
                if (lastJump != 3) {
                    // 进行按钮样式的修改
                    mineTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                    mineImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                    orderTv.setTextColor(Color.GRAY);
                    orderImg.setColorFilter(Color.GRAY);
                    carTv.setTextColor(Color.GRAY);
                    carImg.setColorFilter(Color.GRAY);
                    LoadingDialog.showLoading(this, "正在获取个人信息...");
                    replaceFragment(driverMineFragment);
                    lastJump = 3;
                }
                break;
        }
    }


    // 进行碎片之间的切换
    private void replaceFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.driver_main_fragments, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }

    // 关闭对话框
    public static void closeProgressDialog() {
        LoadingDialog.stopLoading();
    }

    // 初始化控件
    private void initView() {
        // 进行初始化
        driverMapFragment = new DriverMapFragment();
        driverOrderFragment = new DriverOrderFragment();
        driverMineFragment = new DriverMineFragment();

        carLayout = findViewById(R.id.driver_navigate_car_layout);
        carImg = findViewById(R.id.driver_navigate_car_img);
        carTv = findViewById(R.id.driver_navigate_car_tv);

        orderLayout = findViewById(R.id.driver_navigate_order_layout);
        orderImg = findViewById(R.id.driver_navigate_order_img);
        orderTv = findViewById(R.id.driver_navigate_order_tv);

        mineLayout = findViewById(R.id.driver_navigate_mine_layout);
        mineImg = findViewById(R.id.driver_navigate_mine_img);
        mineTv = findViewById(R.id.driver_navigate_mine_tv);

        carTv.setTextColor(getResources().getColor(R.color.perfect_orange));
        carImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
        orderTv.setTextColor(Color.GRAY);
        orderImg.setColorFilter(Color.GRAY);
        mineTv.setTextColor(Color.GRAY);
        mineImg.setColorFilter(Color.GRAY);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
